﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;


namespace Liwey.Common
{
    public class Net
    {
        #region Network
        [DllImport("wininet")]
        public extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue);
        #endregion

        /// <summary>
        /// 检测本机是否联网
        /// </summary>
        /// <returns></returns>
        public static bool IsConnectedInternet()
        {
            int i = 0;
            return InternetGetConnectedState(out i, 0);
        }

        public static void SendMail(string senderDisplayName, string to, string subject, string body, string fileName = null)
        {
            SmtpClient client = new SmtpClient("smtp.exmail.qq.com");
            client.EnableSsl = true;
            client.Credentials = new System.Net.NetworkCredential("support@liwey.com", "qwert123"); 
            
            MailMessage msg = new MailMessage();
            msg.IsBodyHtml = true;
            msg.From = new MailAddress("support@liwey.com", senderDisplayName);
            msg.SubjectEncoding = System.Text.Encoding.UTF8;
            msg.BodyEncoding = System.Text.Encoding.UTF8;
            msg.To.Add(to);
            msg.Subject = subject;
            msg.Body = body;
            if (fileName != null)
            {
                Attachment attachment = new Attachment(fileName);
                msg.Attachments.Add(attachment);
                client.Send(msg);
                attachment.Dispose();
            }
            else
            {
                client.Send(msg);
            }
        }

        public static void SendToSupport(string senderDisplayName, string subject, string body, string fileName = null)
        {
            SendMail(senderDisplayName, "support@liwey.com", subject, body, fileName);
        }
    }
}
